package csc.daonjpa.java.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import csc.daonjpa.java.dao.AccountDAO;
import csc.daonjpa.java.dao.CardDAO;
import csc.daonjpa.java.dao.CustomerDAO;
import csc.daonjpa.java.dao.TargetAccountDAO;
import csc.daonjpa.java.dao.TransactionDAO;
import csc.daonjpa.java.domain.Account;
import csc.daonjpa.java.domain.Card;
import csc.daonjpa.java.domain.Customer;


@Controller
public class AccountController {

	@RequestMapping(value = "/createAccount")
	public ModelAndView addAccount(HttpServletRequest request, Model model) {
		// Read account info from request and save into Account object
		CustomerDAO cus = new CustomerDAO();
		ModelAndView model1 = new ModelAndView("forward:/home.html");
		String loginId = request.getSession().getAttribute("loginSession")
				.toString();
		String accountType = request.getParameter("accountType");
		String accountName = request.getParameter("accountName");
		String accountNumber = request.getParameter("accountNumber");
		int accountAmount = Integer.parseInt(request.getParameter("accountAmount"));
		//String cardType = request.getParameter("cardType");
		
		Customer customer = new Customer();
		customer = cus.getCustomerByLoginId(request.getSession()
				.getAttribute("loginSession").toString());
		Account account = new Account(accountNumber, accountName, accountType,
				accountAmount, customer);

		// Create an AccountDAO
		AccountDAO accountDAO = new AccountDAO();

		// Save account to DB

		boolean a = accountDAO.addAccount(account, loginId);
		if (a == false) {
			model1.addObject("ERROR_CODE", "You are getting Error!!!"
					+ "\n*Please input Data."
					+ "\n*Account Number must be unique."
					);
			return model1;
		}

		return model1;
	}
	
	@RequestMapping(value = "/addCard")
	public ModelAndView addCard(HttpServletRequest request, Model model) {
		ModelAndView model1 = new ModelAndView("addCard");
		String message = "";
		AccountDAO accountDao = new AccountDAO();
		String loginId = request.getSession().getAttribute("loginSession")
				.toString();
		CardDAO cardDao = new CardDAO();
		try {
			List<Account> accountList = accountDao.getAccounts(loginId);
			model.addAttribute("listAccount", accountList);
			List<Card> cardList = cardDao.getCards(loginId);
			model.addAttribute("listCard", cardList);
		} catch (Exception e) {
			message = "List Account get Error";
			model.addAttribute("message", message);
			model1 = new ModelAndView("forward:/home.html");
			return model1;
		}
		return model1;
	}
	
	@RequestMapping(value = "/addNewCard")
	public ModelAndView addNewCard(HttpServletRequest request, Model model) {
		ModelAndView model1 = new ModelAndView("addCard");
		String accountNumber = request.getParameter("accountNumber");
		int accountAmount = Integer.parseInt(request.getParameter("accountAmount"));
		String cardType = request.getParameter("cardType");
		Account account = new Account();
		//AccountDAO accDao = new AccountDAO();
		//account = accDao.getAccountByAccno(accountNumber);
		Card card = new Card(accountNumber,accountAmount,cardType,account);
		CardDAO cardDao = new CardDAO();
		boolean b = cardDao.addCard(card, accountNumber);
		if (b == false) {
			model1.addObject("ERROR_CODE", "You are getting Error!!!"
					+ "\n*Please input Data."
					+ "\n*Account Number must be unique."
					);
			return model1;
		}
		String message = "";
		AccountDAO accountDao = new AccountDAO();
		String loginId = request.getSession().getAttribute("loginSession")
				.toString();
		//CardDAO cardDao = new CardDAO();
		try {
			List<Account> accountList = accountDao.getAccounts(loginId);
			model.addAttribute("listAccount", accountList);
			List<Card> cardList = cardDao.getCards(loginId);
			model.addAttribute("listCard", cardList);
		} catch (Exception e) {
			message = "List Account get Error";
			model.addAttribute("message", message);
			model1 = new ModelAndView("forward:/home.html");
			return model1;
		}
		return model1;
	}
}
